﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CayleyTree {
    public partial class MainForm : System.Windows.Forms.Form {
        private Graphics graphics;
        int n = 10;
        double leng = 100;
        double per1 = 0.6;
        double per2 = 0.7;
        double th1 = 30 * Math.PI / 180;
        double th2 = 20 * Math.PI / 180;
        Color color = Color.Red;

        public MainForm() {
            InitializeComponent();
        }
        private void MainForm_Load(object sender, EventArgs e) {
            comboBox_Color.SelectedIndex = 0;
        }

        private void button_Draw_Click(object sender, EventArgs e) {
            if (graphics == null) {
                graphics = MainSplitContainer.Panel1.CreateGraphics();
            }
            graphics.Clear(MainSplitContainer.Panel1.BackColor);
            drawCayleyTree(n, 200, 360, leng, -Math.PI / 2);
        }

        void drawCayleyTree(int n, double x0, double y0, double leng, double th) {
            if (n == 0) {
                return;
            }
            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);
            drawLine(x0, y0, x1, y1);
            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }

        void drawLine(double x0, double y0, double x1, double y1) {
            graphics.DrawLine(
                new Pen(color),
                (int)x0, (int)y0, (int)x1, (int)y1);
        }

        private void numericUpDown_n_ValueChanged(object sender, EventArgs e) {
            n = (int)numericUpDown_n.Value;
        }

        private void numericUpDown_leng_ValueChanged(object sender, EventArgs e) {
            leng = (int)numericUpDown_leng.Value;
        }

        private void numericUpDown_per1_ValueChanged(object sender, EventArgs e) {
            per1 = (double)numericUpDown_per1.Value;
        }

        private void numericUpDown_per2_ValueChanged(object sender, EventArgs e) {
            per2 = (double)numericUpDown_per2.Value;
        }

        private void numericUpDown_th1_ValueChanged(object sender, EventArgs e) {
            th1 = (double)numericUpDown_th1.Value * Math.PI / 180;
        }

        private void numericUpDown_th2_ValueChanged(object sender, EventArgs e) {
            th2 = (double)numericUpDown_th2.Value * Math.PI / 180;
        }

        private void comboBox_Color_SelectedIndexChanged(object sender, EventArgs e) {
            switch (comboBox_Color.SelectedIndex) {
                case 0:
                    color = Color.Red;
                    break;
                case 1:
                    color = Color.Orange;
                    break;
                case 2:
                    color = Color.Yellow;
                    break;
                case 3:
                    color = Color.Green;
                    break;
                case 4:
                    color = Color.Blue;
                    break;
                case 5:
                    color = Color.Cyan;
                    break;
                case 6:
                    color = Color.Purple;
                    break;
            }
            pictureBox_ColorPreView.BackColor = color;
        }

        private void button_ColorSelect_Click(object sender, EventArgs e) {
            comboBox_Color.SelectedIndex = 7;
            ColorDialog ColorForm = new ColorDialog();
            if (ColorForm.ShowDialog() == DialogResult.OK) {
                color = ColorForm.Color;
                pictureBox_ColorPreView.BackColor = color;
            }
        }
    }
}
